//
//  LookingPassViewController.m
//  sklive
//
//  Created by Macx on 2017/5/8.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "LookingPassViewController.h"

#import "NSString+Common.h"
#import "CountDownTool.h"
#import "TabBarViewController.h"

@interface LookingPassViewController ()<UITextFieldDelegate>

@end

@implementation LookingPassViewController {
	UITextField *_userName;
	UITextField *_code;
	UITextField *_newPassWord;
	UITextField *_repeatPassWord;
	
}

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	
	[self buildUI];
	
}

- (void)createTF {
	
}

- (void)xxx {
	NSLog(@"nil");
}

-(void)popAction{
	[self.navigationController popViewControllerAnimated:YES];
}

#pragma mark ==========构建UI==========
- (void)buildUI {
	
	UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	[returnBtn setImage:IMAGE(@"return") forState:UIControlStateNormal];
	[returnBtn addTarget:self action:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];
	returnBtn.frame = CGRectMake(0, 0, 44, 44);
	self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:returnBtn];
	
	self.navigationItem.title = @"找回密码";
	self.view.backgroundColor = kWhiteColor;
	
	NSArray *arr = @[@"手 机 号：", @"验 证 码：", @"新 密 码：", @"确认密码："];
	NSArray *placeHolder = @[@"点击输入手机号",@"请填写验证码",@"请输入新密码",@"再次确认新密码"];
	
	CGFloat Y = 0.0f;
	
	// 如果type 存在 就是绑定手机
	if (self.type) {
		self.navigationItem.title = @"手机绑定";
		arr = @[@"手机号：", @"验证码："];
		placeHolder = @[@"点击输入手机号",@"请填写验证码"];
		Y = SW(8);
		
		if (self.one) {
			Y = SW(8);
			
			self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:IMAGE(@"") style:UIBarButtonItemStyleDone target:self action:@selector(xxx)];
			
			UIButton * right = [UIButton buttonWithType:UIButtonTypeCustom];
			[right setTitle:@"跳过" forState:UIControlStateNormal];
			right.titleLabel.font = kUHSystemFontWithSize(14);
			right.frame = CGRectMake(0, 0, [LZCTools textSizeWithText:right.titleLabel.text font:right.titleLabel.font].width, 44);
			right.titleLabel.textAlignment = NSTextAlignmentRight;
			[right setTitleColor:kWhiteColor forState:UIControlStateNormal];
			[right addTarget:self action:@selector(skipBinding:) forControlEvents:UIControlEventTouchUpInside];
			self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:right];
		}
	}
	else
		Y = 64+SW(8);
	
	CGSize size = [LZCTools textSizeWithText:@"测字数试试" font:kUHSystemFontWithSize(14)];
	
	// 1
	UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(30, Y, size.width, size.height)];
	[self.view addSubview:label1];
	label1.textColor = kGrayColor;
	label1.textAlignment = NSTextAlignmentCenter;
	label1.text = arr[0];
	label1.font = kUHSystemFontWithSize(14);
	
	_userName = [[UITextField alloc] initWithFrame:CGRectMake(30+size.width+10, Y, SCREEN_WIDTH*2/3, size.height)];
	[self.view addSubview:_userName];
	_userName.delegate = self;
	//    _userName.tag = 11+i;
	_userName.textColor = kGrayColor;
	_userName.clearButtonMode = UITextFieldViewModeWhileEditing;
	_userName.font = kUHSystemFontWithSize(14);
	_userName.placeholder = placeHolder[0];
	
	UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(20, Y+size.height+5, SCREEN_WIDTH-40, 1)];
	[self.view addSubview:line1];
	line1.backgroundColor = kLightGrayColor;
	
	// 2
	UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(30, Y+SW(8)*1, size.width, size.height)];
	[self.view addSubview:label2];
	label2.textColor = kGrayColor;
	label2.textAlignment = NSTextAlignmentCenter;
	label2.text = arr[1];
	label2.font = kUHSystemFontWithSize(14);
	
	_code = [[UITextField alloc] initWithFrame:CGRectMake(30+size.width+10, Y+SW(8)*1, SCREEN_WIDTH*2/3, size.height)];
	[self.view addSubview:_code];
	_code.delegate = self;
	_code.textColor = kGrayColor;
	_code.clearButtonMode = UITextFieldViewModeWhileEditing;
	_code.font = kUHSystemFontWithSize(14);
	_code.placeholder = placeHolder[1];
	
	UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(20, Y+size.height+5+SW(8)*1, SCREEN_WIDTH-40, 1)];
	[self.view addSubview:line2];
	line2.backgroundColor = kLightGrayColor;
	
	UIView *line0;
	
	if (!self.type) {
		
		// 3
		UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(30, Y+SW(8)*2, size.width, size.height)];
		[self.view addSubview:label3];
		label3.textColor = kGrayColor;
		label3.textAlignment = NSTextAlignmentCenter;
		label3.text = arr[2];
		label3.font = kUHSystemFontWithSize(14);
		
		_newPassWord = [[UITextField alloc] initWithFrame:CGRectMake(30+size.width+10, Y+SW(8)*2, SCREEN_WIDTH*2/3, size.height)];
		[self.view addSubview:_newPassWord];
		_newPassWord.delegate = self;
		_newPassWord.textColor = kGrayColor;
		_newPassWord.clearButtonMode = UITextFieldViewModeWhileEditing;
		_newPassWord.font = kUHSystemFontWithSize(14);
		_newPassWord.placeholder = placeHolder[2];
		
		UIView *line3 = [[UIView alloc] initWithFrame:CGRectMake(20, Y+size.height+5+SW(8)*2, SCREEN_WIDTH-40, 1)];
		[self.view addSubview:line3];
		line3.backgroundColor = kLightGrayColor;
		
		// 4
		UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(30, Y+SW(8)*3, size.width, size.height)];
		[self.view addSubview:label4];
		label4.textColor = kGrayColor;
		label4.textAlignment = NSTextAlignmentCenter;
		label4.text = arr[3];
		label4.font = kUHSystemFontWithSize(12);
		
		_repeatPassWord = [[UITextField alloc] initWithFrame:CGRectMake(30+size.width+10, Y+SW(8)*3, SCREEN_WIDTH*2/3, size.height)];
		[self.view addSubview:_repeatPassWord];
		_repeatPassWord.delegate = self;
		_repeatPassWord.textColor = kGrayColor;
		_repeatPassWord.clearButtonMode = UITextFieldViewModeWhileEditing;
		_repeatPassWord.font = kUHSystemFontWithSize(14);
		_repeatPassWord.placeholder = placeHolder[3];
		
		UIView *line4 = [[UIView alloc] initWithFrame:CGRectMake(20, Y+size.height+5+SW(8)*3, SCREEN_WIDTH-40, 1)];
		[self.view addSubview:line4];
		line4.backgroundColor = kLightGrayColor;
		
		line0 = line4;
	}
	else
		line0 = line2;
	
	
	//    for (int i = 0; i < arr.count; i++) {
	//
	//        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, Y+SW(8)*i, size.width, size.height)];
	//        [self.view addSubview:label];
	//        label.textColor = kGrayColor;
	//        label.textAlignment = NSTextAlignmentCenter;
	//        label.text = arr[i];
	//        label.font = kUHSystemFontWithSize(14);
	//
	//        UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(30+size.width+10, Y+SW(8)*i, SCREEN_WIDTH*2/3, size.height)];
	//        [self.view addSubview:field];
	//        field.delegate = self;
	//        field.tag = 11+i;
	//        field.textColor = kGrayColor;
	//        field.clearButtonMode = UITextFieldViewModeWhileEditing;
	//        field.font = kUHSystemFontWithSize(14);
	//        if (i == 0) {
	//            field.keyboardType = UIKeyboardTypeNumberPad;
	//        }
	//        if (i == 2 || i == 3) {
	//            field.secureTextEntry = YES;
	//        }
	//
	//        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(20, Y+size.height+5+SW(8)*i, SCREEN_WIDTH-40, 1)];
	//        [self.view addSubview:line];
	//        line.backgroundColor = kLightGrayColor;
	//        line.tag = 21+i;
	//
	//    }
	//
	//    _userName = [self.view viewWithTag:11];
	//    _code = [self.view viewWithTag:12];
	//    _newPassWord = [self.view viewWithTag:13];
	//    _repeatPassWord = [self.view viewWithTag:14];
	
	//    UIView *line2 = [self.view viewWithTag:24];
	//    if (self.type) {
	//        line2 = [self.view viewWithTag:22];
	//    }
	
	UIButton *codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	[self.view addSubview:codeBtn];
	[codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
	codeBtn.titleLabel.font = kUHSystemFontWithSize(12);
	[codeBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
	[codeBtn addTarget:self action:@selector(getCodeClick:) forControlEvents:UIControlEventTouchUpInside];
	CGSize codeSize = [LZCTools textSizeWithText:codeBtn.titleLabel.text font:kUHSystemFontWithSize(13)];
	codeBtn.layer.cornerRadius = 3;
	codeBtn.layer.masksToBounds = YES;
	codeBtn.backgroundColor = UIMainColorRGB;
	codeBtn.frame = CGRectMake(SCREEN_WIDTH-codeSize.width-50, _code.centerY-codeSize.height/2-3, codeSize.width+20, codeSize.height+6);
	
	UIButton *completeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	[self.view addSubview:completeBtn];
	[completeBtn setTitle:@"完成" forState:UIControlStateNormal];
	[completeBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
	CGFloat btnHeight = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(14)].height+SW(20);
	completeBtn.frame = CGRectMake(20, line0.y+1+20, SCREEN_WIDTH-40, btnHeight);
	completeBtn.backgroundColor = UIMainColorRGB;
	completeBtn.layer.cornerRadius = 5;
	completeBtn.layer.masksToBounds = YES;
	[completeBtn addTarget:self action:@selector(completeClick:) forControlEvents:UIControlEventTouchUpInside];
	
}

#pragma mark ===========响应方法==========
/**
 获取验证码
 */
- (void)getCodeClick:(UIButton *)sender {
	NSLog(@"获取验证码");
	
	// 手机号不为空
	if ([NSString isEmpty:_userName.text]) {
		[self presentViewController:[LZCTools alertMessage:@"手机号不可为空"] animated:YES completion:nil];
		return;
	}
	// 手机号码输入验证
	if (![NSString isValidatePhoneNumber:_userName.text]) {
		[self presentViewController:[LZCTools alertMessage:@"您输入的不是手机号"] animated:YES completion:nil];
		return;
	}
	
	[[NetworkTool sharedTool] requestWithName:@"token" URLString:APIMobileCodes parameters:@{@"mobile":_userName.text} method:@"GET" callBack:^(id responseObject) {
	}];
	
	// 倒计时
	[CountDownTool timeCountDown:sender second:60];
}
/**
 完成密码响应
 */
- (void)completeClick:(id)sender {
	NSLog(@"完成新密码响应");
	
	// 手机号不为空
	if ([NSString isEmpty:_userName.text]) {
		[self presentViewController:[LZCTools alertMessage:@"手机号不可为空"] animated:YES completion:nil];
		return;
	}
	// 手机号码输入验证
	if (![NSString isValidatePhoneNumber:_userName.text]) {
		[self presentViewController:[LZCTools alertMessage:@"您输入的不是手机号"] animated:YES completion:nil];
		return;
	}
	if (self.type) {
		NSDictionary *param = @{@"mobile":_userName.text,
								@"mobile_code":_code.text};
		
		[[NetworkTool sharedTool] requestWithName:@"" URLString:APIMobiles parameters:param method:@"PUT" callBack:^(id responseObject) {
			if (self.one == 1) {
				AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
				app.window.rootViewController = [[TabBarViewController alloc] init];
			}
			else {
				if (self.returnPhone != nil) {
					self.returnPhone(_userName.text);
				}
				[self.navigationController popViewControllerAnimated:YES];
			}
		}];
		
	}
	else if (!self.type) {
		// 密码不为空
		if ([NSString isEmpty:_newPassWord.text] || [NSString isEmpty:_repeatPassWord.text]) {
			[self presentViewController:[LZCTools alertMessage:@"密码不可为空"] animated:YES completion:nil];
			return;
		}
		// 密码长度要大于6
		if (_repeatPassWord.text.length < 6 || _newPassWord.text.length < 6) {
			[self presentViewController:[LZCTools alertMessage:@"密码不可以少于6位数"] animated:YES completion:nil];
			return;
		}
		// 验证码不为空
		if ([NSString isEmpty:_code.text]) {
			[self presentViewController:[LZCTools alertMessage:@"验证码不可为空"] animated:YES completion:nil];
			return;
		}
		// 密码必须相同
		if (![_repeatPassWord.text isEqualToString:_newPassWord.text]) {
			[self presentViewController:[LZCTools alertMessage:@"密码不相同，无效！"] animated:YES completion:nil];
			return;
		}
		
		NSDictionary *param = @{@"mobile":_userName.text,
								@"new_password":_newPassWord.text,
								@"mobile_code":_code.text};
		
		[[NetworkTool sharedTool] requestWithName:@"密码找回" URLString:APIPassWords parameters:param method:@"PUT" callBack:^(id responseObject) {
			[self.navigationController popViewControllerAnimated:YES];
		}];
		
	}
}

- (void)skipBinding:(UIButton *)sender {
	AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
	app.window.rootViewController = [[TabBarViewController alloc] init];
}

- (void)returnPhone:(ReturnPhoneBlock)block {
	self.returnPhone = block;
}

#pragma mark ===========代理方法==========
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[textField resignFirstResponder];
	return YES;
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
